import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:read_book/utils/cover_image.dart';

class SwiperPage extends StatefulWidget {
  // 获取父组件的值
  SwiperPage({Key key, this.books}) : super(key: key);
  List books; // 图片数组
  @override
  _SwiperPageState createState() {
    return _SwiperPageState();
  }
}

class _SwiperPageState extends State<SwiperPage> {
  //排行id
  final String totalRank = '564d8494fe996c25652644d2';
  List swiperList; //图片数组

  void getData() {
    List arr = [];
    for (int i = 0; i < widget.books.length; i++) {
      if (i < 3) {
        arr.add(CoverImage.convertImageUrl(widget.books[i]['cover']));
      }
    }
    setState(() {
      swiperList = arr;
    });
  }

  @override
  void initState() {
    super.initState();
    getData();
    if (kDebugMode) {
      print(swiperList);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SizedBox(
        width: MediaQuery.of(context).size.width,
        height: 200.0,
        child: Swiper(
          itemBuilder: _swipeBuilder,
          itemCount: 3,
          pagination: const SwiperPagination(
              builder: DotSwiperPaginationBuilder(
                  color: Colors.black54, activeColor: Colors.white)),
          scrollDirection: Axis.horizontal,
          autoplay: true,
          onTap: (index) => print('点击了第$index个'),
        ),
      ),
    );
  }

  Widget _swipeBuilder(BuildContext context, int index) {
    return Image.network(
      swiperList[index],
      scale: 8.5,
      fit: BoxFit.contain,
    );
  }
}
